// Copyright@ChenChao


#include "Player/CPlayerCharacter.h"

#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "EnhancedInputSubsystems.h"
#include "EnhancedInputComponent.h"

ACPlayerCharacter::ACPlayerCharacter()
{
	/*让角色自己转到方向，不用鼠标旋转*/
	bUseControllerRotationPitch = false;
	bUseControllerRotationYaw = false;
	bUseControllerRotationRoll = false;
	/*弹簧臂组件*/
	CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
	CameraBoom->SetupAttachment(GetRootComponent());		//设置父级（根组件）
	CameraBoom->TargetArmLength = 800.f;		//设置臂长
	CameraBoom->SocketOffset = FVector(0.f, 0.f, 0.f);		//插槽偏移
	CameraBoom->bUsePawnControlRotation = true;		//使用棋子控制旋转

	/*相机组件*/
	FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
	FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);		//设置父级（弹簧臂插槽名称）
	FollowCamera->bUsePawnControlRotation = false;
	
	/*角色移动组件*/
	GetCharacterMovement()->bOrientRotationToMovement = true;		//保持旋转到位移方向
	GetCharacterMovement()->RotationRate = FRotator(0.f, 720.f, 0.f);		//旋转速度（转身速度）
	GetCharacterMovement()->MaxWalkSpeed = 400.f;		//最大位移速度
	GetCharacterMovement()->BrakingDecelerationWalking = 200.f;		//行走加速度
}

void ACPlayerCharacter::PawnClientRestart()
{
	Super::PawnClientRestart();

	// 获取玩家控制器
	APlayerController* OwningPlayerController = Cast<APlayerController>(GetController());
	if (OwningPlayerController)
	{
		//获取本地玩家子系统
		class ULocalPlayer* LocalPlayer = OwningPlayerController->GetLocalPlayer();
		//获取本地玩家子系统
		UEnhancedInputLocalPlayerSubsystem* Subsystem = LocalPlayer->GetSubsystem<UEnhancedInputLocalPlayerSubsystem>();
		if (Subsystem)
		{
			checkf(InputContext, TEXT("InputContext未配置资产！"));
			//先移除旧的输入上下文
			Subsystem->RemoveMappingContext(InputContext);
			//绑定映射情境
			Subsystem->AddMappingContext(InputContext, 0);
		}
	}
}

void ACPlayerCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
	Super::SetupPlayerInputComponent(PlayerInputComponent);

	//增强输入组件
	UEnhancedInputComponent* EnhancedInputComponent = Cast<UEnhancedInputComponent>(PlayerInputComponent);
	if (EnhancedInputComponent)
	{
		checkf(InputAction_Jump, TEXT("InputAction_Jump未配置资产！"));
		checkf(InputAction_Look, TEXT("InputAction_Look未配置资产！"));
		checkf(InputAction_Move, TEXT("InputAction_Move未配置资产！"));
		//动作绑定
		EnhancedInputComponent->BindAction(InputAction_Jump, ETriggerEvent::Triggered, this, &ACPlayerCharacter::Jump);
		EnhancedInputComponent->BindAction(InputAction_Look, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleLookInput);
		EnhancedInputComponent->BindAction(InputAction_Move, ETriggerEvent::Triggered, this, &ACPlayerCharacter::HandleMoveInput);
	}
}

void ACPlayerCharacter::HandleLookInput(const FInputActionValue& InputActionValue)
{
	//获取鼠标轴二维向量
	const FVector2D InputValue = InputActionValue.Get<FVector2D>();
	//左右看
	if (InputValue.X != 0.f)
	{
		AddControllerYawInput(InputValue.X);
	}
	// 上下看
	if (InputValue.Y != 0.f)
	{
		AddControllerPitchInput(InputValue.Y);
	}
}

void ACPlayerCharacter::HandleMoveInput(const FInputActionValue& InputActionValue)
{
	//获取鼠标轴二维向量
	const FVector2D InputValue = InputActionValue.Get<FVector2D>();
	//结果归一化
	InputValue.GetSafeNormal();

	AddMovementInput(GetMoveForwardDir()*InputValue.Y+GetLookRightDir()*InputValue.X);
}

FVector ACPlayerCharacter::GetLookRightDir() const
{
	return FollowCamera->GetRightVector();
}

FVector ACPlayerCharacter::GetLookForwardDir() const
{
	return FollowCamera->GetForwardVector();
}

FVector ACPlayerCharacter::GetMoveForwardDir() const
{
	return FVector::CrossProduct(GetLookRightDir(), FVector::UpVector);
}
